package com.wmf.servlet;

import com.wmf.model.User;
import com.wmf.service.UserService;
import com.wmf.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String con = req.getParameter("con");

        try {
            if(con == "reg" || con.equals("reg")){
                register(req,resp);
            }
            if(con == "val" || con.equals("val")){
                validateInfo(req,resp);
            }
            if(con == "login" || con.equals("login")){
                login(req,resp);
            }
            if(con == "show" || con.equals("show")){
                showUserInfo(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //前台展示用户数据
    private void showUserInfo(HttpServletRequest req, HttpServletResponse resp) {
        UserService userService = new UserServiceImpl();
        List<User> userList = userService.getUserInfo();
        req.setAttribute("userList",userList);
        try {
            req.getRequestDispatcher("/view/my.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //注册
    public void register(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        User user = new User();
        UserService userService = new UserServiceImpl();
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        user.setName(name);
        user.setPhone(phone);
        user.setPassword(password);
        int flag = userService.register(user);
        if(flag == 1){
            resp.getWriter().write("yes");
        }else{
            resp.getWriter().write("no");
        }
        System.out.println(flag);
    }

    //验证方法、注册时候用来验证信息
    public void validateInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        UserService userService = new UserServiceImpl();
        String res = "";
        List<User> userList = userService.getUserInfo();
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        int flag = 0;
        for (User user:userList) {
            if( name == user.getName() || name.equals(user.getName())){
                resp.getWriter().write("nameis");
                flag = 1;
                break;

            }
            if(phone == user.getPhone() || phone.equals(user.getPhone())){
                resp.getWriter().write("phis");
                flag = 1;
                break;
            }

        }
        if(flag == 0){
            resp.getWriter().write("yes");
        }
    }

    //登录验证
    public void login(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        UserService userService = new UserServiceImpl();
        String res = "";
        List<User> userList = userService.getUserInfo();

        String phone = req.getParameter("phone");
        String pwd = req.getParameter("pwd");

        int flag = 0;
        for (User user:userList) {
            //说明用户存在
            if( (phone == user.getName() || phone.equals(user.getName()))
                || (phone == user.getPhone() || phone.equals(user.getPhone()))){
                if(pwd == user.getPassword() || pwd.equals(user.getPassword())){
                    flag = 1;
                    //放到session中,用来验证是否已经登录成功
                    req.getSession().setAttribute("flag","loginsucc");
                    String info = (String) req.getSession().getServletContext().getAttribute("phone");
                    if(info==null){
                        req.getSession().getServletContext().setAttribute("phone",phone);
                        //统计登录人数
                        Integer logincount = (Integer)  req.getSession().getServletContext().getAttribute("logincount");
                        req.getSession().getServletContext().setAttribute("logincount",logincount+1);
                    }
                    resp.getWriter().write("yes");
                    break;
                }else{
                    flag = 1;
                    resp.getWriter().write("pwderr");
                    break;
                }
            }

        }
        if(flag == 0){
            resp.getWriter().write("noname");
        }
    }

    //获取所有用户方法
    public List<User> getUserInfo() {
        UserService userService = new UserServiceImpl();
        List<User> userList = userService.getUserInfo();
        return userList;
    }
}
